<?php
namespace App\Controller;

class LevelArosController extends AppController{

    public $_page_num = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：获取优惠信息
    * */
    public function getInfo()
    {
        $id = $this->request->getData('id');
        $deal_points = $this->DealPoints->findById($id)->first();
        if(empty($deal_points)){
            $this->G->error("deal_point_error");
            return null;
        }
        $deal_points['worth'] = $deal_points['worth']/100;
        $deal_points['begin_time'] = date('Y-m-d H:i:s', $deal_points['begin_time']);
        $deal_points['end_time'] = date('Y-m-d H:i:s', $deal_points['end_time']);
        $this->G->success("success", $deal_points);
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：删除会员组
    * */
    public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $this->LevelAros->query()
            ->update()
            ->set(['is_delete'=>1])
            ->where(['id' => $id])
            ->execute();
        $this->G->success("success");
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：获取会员组(启用、禁用)列表
    * */
    public function getList()
    {
        $type = $this->request->getData('type');
        if(empty($type) || !in_array($type,['allowed','denied'])){
            $this->G->error("type_error");
            return null;
        }
        $levelAros = $this->LevelAros->find()->toArray();
        $re = [];
        $re['list'] = [];
        if(!empty($deal_points)){
            foreach ($deal_points as $k=>$v){
                $v['worth'] = $v['worth']/100;
                $v['begin_time'] = date('Y-m-d H:i:s', $v['begin_time']);
                $v['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
                $re['list'][] = $v;
            }
        }
        $this->G->success("Success", $re);
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：修改会员组
    * */
    public function update()
    {
        $alias = $this->request->getData('alias');
        $acos = $this->request->getData('acos');
        $id = $this->request->getData('id');
        $type = $this->request->getData('type');
//        if(empty($alias)){
//            $this->G->error("alias_error");
//            return null;
//        }
//        if(empty($acos)){
//            $this->G->error("acos_error");
//            return null;
//        }
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        if(empty($type) || !in_array($type,['allowed','denied'])){
            $this->G->error("type_error");
            return null;
        }
        $levelAros = $this->LevelAros->findById($id)->select(['id'])->first();
        if(empty($levelAros)){
            $this->G->error("levelAros_error");
            return null;
        }
        $levelAro = $this->LevelAros->newEntity();
        $levelAro->id = $id;
        if(!empty($alias)){
            $levelAro->alias = $alias;
        }
        if(!empty($acos) && $type ==='allowed'){
            $levelAro->allowed = json_encode($acos);
        }
        if(!empty($acos) && $type ==='denied'){
            $levelAro->denied = json_encode($acos);
        }
        $levelAro->update_user = $this->Jwt->get('user_id');
//        $levelAro->update_time = time();
        $this->LevelAros->save($levelAro);
        $this->G->success("Success");
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：新增会员组
    * */
    public function add()
    {
        $alias = $this->request->getData('alias');
        $acos = $this->request->getData('acos');
        $type = $this->request->getData('type');
//        if(empty($alias)){
//            $this->G->error("alias_error");
//            return null;
//        }
//        if(empty($acos)){
//            $this->G->error("acos_error");
//            return null;
//        }
        $levelAro = $this->LevelAros->newEntity();
        if(!empty($alias)){
            $levelAro->alias = $alias;
        }
        if(!empty($acos) && $type ==='allowed'){
            $levelAro->allowed = json_encode($acos);
        }
        if(!empty($acos) && $type ==='denied'){
            $levelAro->denied = json_encode($acos);
        }
        $levelAro->create_user = $this->Jwt->get('user_id');
//        $levelAro->create_time = time();
        $levelAro->is_delete = 0;
        $levelAro->enabled = 1;
        $this->LevelAros->save($levelAro);
        $this->G->success("Success");
    }

}
?>